var obj = queryStr();
var page = 1;
var pageSize = 2;
var totalCount = 0;
var totalPage = 0;
var str = '';
var price = 1;
var num = null;
var proName = obj['key'];
var data = {
    page: page,
    pageSize: pageSize,
    price: price,
    proName: proName,
    num: num
}
var That = null;
getGoods(data)

function getGoods(params, callback) {
    $.ajax({
        url: '/product/queryProduct',
        data: params,
        success: function(res) {
            pageSize = res.size,
                totalCount = res.count,
                totalPage = Math.ceil(totalCount / pageSize)
            var html = template('goodsTmp', res)
            str += html
            $('.product').html(str)
            callback && callback()
        }
    })
}

// 升序排序降序排序
$('.topNav').on('tap', 'span', function() {
    var val = $(this).attr('val')
    str = ''
    page = 1
    if (val == 'price') {
        price = price == 1 ? 2 : 1
    } else {
        num = num == 1 ? 2 : 1
    }

    data['page'] = page
    data['num'] = num
    data['price'] = price
    mui("#refreshContainer").pullRefresh().refresh(true);
    getGoods(data, function() {
        That.endPullupToRefresh(false)
    })
})


// 跳转商品详情页面
$('.product').on('tap', 'li', function() {
    window.location.href = $(this).attr('href')
})


// 封装ajax请求
var refreshContainer = document.querySelector("#refreshContainer");
mui.init({
    pullRefresh: {
        container: refreshContainer,
        down: {
            auto: false, //可选,默认false.首次加载自动下拉刷新一次
            contentdown: "下拉可以刷新", //可选，在下拉可刷新状态时，下拉刷新控件上显示的标题内容
            contentover: "释放立即刷新", //可选，在释放可刷新状态时，下拉刷新控件上显示的标题内容
            contentrefresh: "正在刷新...", //可选，正在刷新状态时，下拉刷新控件上显示的标题内容
            callback: pulldownRefresh
        },
        up: {
            contentrefresh: '正在加载...',
            callback: pullupRefresh
        }
    }
});
/**
 * 下拉刷新具体业务实现
 */
function pulldownRefresh() {
    That = this;
    page = 1;
    str = '';
    data.page = page;
    getGoods(data, function() {
        mui("#refreshContainer").pullRefresh().endPulldownToRefresh();
        mui('#refreshContainer').pullRefresh().enablePullupToRefresh();
    })
}
/**
 * 上拉加载具体业务实现
 */

function pullupRefresh() {
    That = this;
    page++;
    if (page > totalPage) {
        mui("#refreshContainer").pullRefresh().endPullupToRefresh(true);
        mui('#refreshContainer').pullRefresh().disablePullupToRefresh();
    } else {
        data.page = page;
        getGoods(data, function() {
            That.endPullupToRefresh(false);
        })
    }
}